package com.styytech.dadabao.ui.my.accountset;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.ui.login.UpdateLoginPasswordActivity;
import com.styytech.dadabao.ui.main.GesturePasswordActivity;
import com.styytech.dadabao.ui.main.MainActivity;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.SpUser;
import com.styytech.dadabao.widge.dialog.CustomAlertDialog;

/**
 * 账户设置
 */
@ContentView(R.layout.activity_account_set)
public class AccountSetActivity extends BaseActivity {

    /*银行卡管理*/
    @ViewInject(R.id.llyt_bankcard_manage)
    LinearLayout llyt_bankcard_manage;
    /*银行卡管理未设置*/
    @ViewInject(R.id.tv_bankcard_set)
    TextView tv_bankcard_set;
    /*安全设置*/
    @ViewInject(R.id.llyt_safety_set)
    LinearLayout llyt_safety_set;
    /*完全设置未完成*/
    @ViewInject(R.id.tv_safety_set)
    TextView tv_safety_set;
    /*登录密码*/
    @ViewInject(R.id.llyt_login_password)
    LinearLayout llyt_login_password;
    /*登录密码设置*/
    @ViewInject(R.id.tv_login_password)
    TextView tv_login_password;
    /*手势密码*/
    @ViewInject(R.id.llyt_gesture_password)
    LinearLayout llyt_gesture_password;
    /*修改手势密码*/
    @ViewInject(R.id.llyt_update_gesture)
    LinearLayout llyt_update_gesture;
    @ViewInject(R.id.tv_update_gesture_password)
    TextView tv_update_gesture_password;
    /*登录注册按钮*/
    @ViewInject(R.id.llyt_login_regist)
    LinearLayout llyt_login_regist;
    /*手势密码设置*/
    @ViewInject(R.id.iv_switch)
    ImageView iv_switch;
    /*登录*/
    @ViewInject(R.id.bt_login)
    Button bt_login;
    /*注册*/
    @ViewInject(R.id.bt_register)
    Button bt_register;
    /*退出*/
    @ViewInject(R.id.bt_exit)
    Button bt_exit;
    /*修改手势密码上面的分割线*/
    @ViewInject(R.id.v_update_gesture)
    View v_update_gesture;

    /*上下文*/
    private Context mContext;
    private Boolean isOpen;// 手势密码开关
    private int idCardVStatus = 0;//0：未实名认证，1：已实名认证
    private int payforView = 0;//0:未设置支付密码，1：已设置支付密码界面
    private int bindBankCard = 0;//0:未绑定过，1：绑定过
    private int isEnsure = 0;// 0：取消；1:确认；-1：密码不正确
    private int verify = 0;// 0:未验证完 1：已完成
    private boolean isSetGestruePass = false;// 是否设置过手势密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("账户设置", View.VISIBLE);

    }

    /**
     * 初始界面设置
     */
    private void initData() {
        isOpen = SpUser.getGestureSwitch(mContext);
        isSetGestruePass = SpUser.getUserGesturePassword(mContext) != null ? true : false;
        if(isSetGestruePass){
            tv_update_gesture_password.setText("修改手势密码");
        }else{
            tv_update_gesture_password.setText("设置手势密码");
        }
        if (isOpen) {
            iv_switch.setBackgroundResource(R.drawable.switch_open);
            llyt_update_gesture.setVisibility(View.VISIBLE);
            v_update_gesture.setVisibility(View.VISIBLE);
        } else {
            iv_switch.setBackgroundResource(R.drawable.switch_close);
            llyt_update_gesture.setVisibility(View.GONE);
            v_update_gesture.setVisibility(View.GONE);
        }

        /*是否验证过*/
        idCardVStatus = SpUser.getIdCardVStatus(mContext);
        /*是否设置过交易密码*/
        payforView = SpUser.getBuyPasswordStatus(mContext);

        if (idCardVStatus == 1 && payforView == 1) {
            verify = 1;
            tv_safety_set.setText("已完成");
            tv_safety_set.setTextColor(getResources().getColor(R.color.hint_text));
        } else {
            verify = 0;
        }

        bindBankCard = SpUser.getBankCardVStatus(mContext);
        if (bindBankCard == 1) {
            tv_bankcard_set.setText("已设置");
            tv_bankcard_set.setTextColor(getResources().getColor(R.color.hint_text));
        } else {
            tv_bankcard_set.setText("未设置");
            tv_bankcard_set.setTextColor(getResources().getColor(R.color.main_red));
        }
    }

    @OnClick({R.id.llyt_bankcard_manage,
            R.id.llyt_safety_set,
            R.id.llyt_login_password,
//            R.id.llyt_gesture_password,
            R.id.llyt_update_gesture,
            R.id.iv_switch,
            R.id.bt_login,
            R.id.bt_register,
            R.id.bt_exit})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.llyt_bankcard_manage:// 银行卡管理
                if (verify == 1) {
                    startNextActivity(BankCardManageActivity.class);
                } else if (verify == 0) {
                    toast("您还未完成安全设置，请先完成安全设置");
                }
                break;
            case R.id.llyt_safety_set://安全设置
                startNextActivity(SafetySetActivity.class);
                break;
            case R.id.llyt_login_password://登录密码(忘记密码)
                startNextActivity(UpdateLoginPasswordActivity.class);
                break;
            case R.id.llyt_gesture_password://手势密码
                if (SpUser.getGestureSwitch(mContext)) {
                    // 该点击已取消
                } else {
                    toast("手势密码已关闭");
                }
                break;
            case R.id.llyt_update_gesture://修改手势密码
                if(isSetGestruePass){
                    startGesturePasswordActivity(true);// 修改手势密码
                }else{
                    startGesturePasswordActivity(false);// 设置手势密码
                }
                break;
            case R.id.iv_switch://手势密码开关
                displayDialog();
                break;
            case R.id.bt_login://登录
//                startNextActivity(LoginActivity.class);
                break;
            case R.id.bt_register://注册
//                startNextActivity(RegisterActivity.class);
                break;
            case R.id.bt_exit://退出
                SpUser.clearShare(mContext);
                startNextActivity(MainActivity.class);
                finish();
                break;
        }
    }


    /**
     * 显示对话框
     */
    private void displayDialog() {
        String userAccount = SpUser.getUserName(mContext).toString();
        String phone = userAccount.substring(0, 3) + "****" + userAccount.substring(7, 11);
        final CustomAlertDialog mdlg = new CustomAlertDialog(context, "大大宝账户",
                phone);
        mdlg.dlg.setCancelable(false);
        mdlg.btn_ok.setText("确定");
        mdlg.btn_cancel.setText("取消");
        mdlg.btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String password = mdlg.et_password.getText().toString();
                if (checkUserPassword(password)) {
                    gestureSwitch();

                } else {
                    displayErrorDialog();
                }
                CommonUtils.closeDialog(mdlg);
            }

        });
        mdlg.btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                isEnsure = 0;
                CommonUtils.closeDialog(mdlg);
            }
        });
        mdlg.show();
    }

    /**
     * 显示错误警告框
     */
    private void displayErrorDialog() {
        final CustomAlertDialog mdlg = new CustomAlertDialog(context, "温馨提示",
                "密码不正确");
        mdlg.dlg.setCancelable(false);
        mdlg.btn_ok.setText("确定");
        mdlg.btn_cancel.setText("");
        mdlg.et_password.setVisibility(View.GONE);
        mdlg.btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                CommonUtils.closeDialog(mdlg);
            }
        });
        mdlg.show();
    }

    private boolean checkUserPassword(String password) {
        String userPassword = SpUser.getUserInfo(mContext, ConstantsCommon.USER_PASSWORD).toString();
        if (userPassword == null || userPassword == null) {
            return false;
        } else if (password.equals(userPassword)) {
            return true;
        }
        return false;
    }

    /**
     * 设置开关
     */
    private void gestureSwitch() {
        //消息推送开关
        if (isOpen) {
            isOpen = false;
            iv_switch.setBackgroundResource(R.drawable.switch_close);
            llyt_update_gesture.setVisibility(View.GONE);
            v_update_gesture.setVisibility(View.GONE);
        } else {
            isOpen = true;
            iv_switch.setBackgroundResource(R.drawable.switch_open);
            llyt_update_gesture.setVisibility(View.VISIBLE);
            v_update_gesture.setVisibility(View.VISIBLE);
        }
        SpUser.setGestureSwitch(mContext, isOpen);
    }

    /**
     * 启动界面
     *
     * @param cls 需要启动的界面类
     */
    private void startNextActivity(Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
    }

    /**
     * 提示消息
     *
     * @param str 需要提示的消息
     */
    private void toast(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 打开手势密码界面
     *
     * @param bool 是否需要修改手势密码
     */
    private void startGesturePasswordActivity(boolean bool) {
        if (bool == false) {// 如果是设置手势密码
            SpUser.setUserGesturePassword(mContext, null);
        }
        Intent intent = new Intent(mContext, GesturePasswordActivity.class);
        intent.putExtra("UPDATEGESTURE", bool);
        startActivity(intent);
    }
}

